/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2024/6/12
 * Time: 14:34
 */

#include "CardUi.h"

namespace Upgrade {
    CardUi::CardUi(CardSPtr _card, QWidget *_parent /* = nullptr*/)
        : QPushButton(_parent)
        , card_(_card)
    {
        setAttribute(Qt::WA_TranslucentBackground);//设置窗口背景透明
        setWindowFlags(Qt::FramelessWindowHint | Qt::WindowCloseButtonHint);
        setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);// 添加大小策略
        InitState();
    }

    CardSPtr CardUi::Card()
    {
        return card_;
    }

    QSize CardUi::sizeHint() const
    {
        return {150, heightForWidth(150)};
    }

    QSize CardUi::minimumSizeHint() const
    {
        return sizeHint();
    }

    int CardUi::heightForWidth(int width) const
    {
        // width : heigh = 3 : 4
        return static_cast<int>(width * 1.0 / 3 * 4);
    }

    bool CardUi::hasHeightForWidth() const
    {
        return true;
    }

    void CardUi::InitState()
    {
        QString name = GetSVGName(card_->suit);
        auto    data = GetContent(name, card_->color);

        auto textdata = GetTextContent("主","#66bb6a","#ffffff");

        big_renderer_.load(data);
        small_renderer_.load(data);
        joker_renderer_.load(data);
        text_renderer_.load(textdata);
        big_renderer_.setAspectRatioMode(Qt::KeepAspectRatio);
        small_renderer_.setAspectRatioMode(Qt::KeepAspectRatio);
        joker_renderer_.setAspectRatioMode(Qt::KeepAspectRatio);
        text_renderer_.setAspectRatioMode(Qt::KeepAspectRatio);
    }

    void CardUi::paintEvent(QPaintEvent *event)
    {
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing, true);

        // 获取当前窗口大小
        QRect rect   = this->rect();
        int   width  = rect.width();
        int   height = rect.height();

        // 计算圆角半径（相对于窗口大小）
        int radius = qMin(width, height) / 10;

        // 绘制圆角矩形背景
        QPainterPath path;
        path.addRoundedRect(rect, radius, radius);
        painter.fillPath(path, QColor(255, 255, 255));
        painter.setPen(Qt::black);
        painter.drawPath(path);

        // 设置边距和绘制区域相对于窗口大小
        int margin          = width / 20;// 边距相对于宽度
        int smallSymbolSize = height / 6;
        int largeSymbolSize = width / 2;
        int textHeight      = height / 6;// 文本高度相对于高度

        if(card_->suit == CardSuit::JokerSuit) {
            // 绘制左上角的joker
            QRect smallSymbolRect(margin, margin, textHeight, smallSymbolSize);
            small_renderer_.render(&painter, smallSymbolRect);
        }
        else {
            // 绘制左上角的数字
            QFont font = painter.font();
            font.setPointSizeF(textHeight * 0.6);// 设置字号为 textHeight 的 60%
            painter.setFont(font);
            QFontMetrics fm(font);

            painter.setPen(card_->color);
            painter.drawText(QRect(margin, margin, textHeight, height), Qt::AlignHCenter, Rank2String(card_->rank));

            int actualTextHeight = fm.height();// 获取实际的文字高度

            // 绘制左上角的花色
            QRect smallSymbolRect(margin, margin + actualTextHeight, smallSymbolSize, smallSymbolSize);
            small_renderer_.render(&painter, smallSymbolRect);
        }
        // 绘制中间的大花色
        QRect largeSymbolRect((width - largeSymbolSize) / 2, (height - largeSymbolSize) / 2, largeSymbolSize, largeSymbolSize);
        big_renderer_.render(&painter, largeSymbolRect);

        // 绘制左下角“主”
        if(card_->IsTrump()) {
            QRect textRect(margin, height - margin - textHeight, textHeight, smallSymbolSize);
            text_renderer_.render(&painter, textRect);
        }

        QWidget::paintEvent(event);
    }

    void CardUi::mousePressEvent(QMouseEvent *event)
    {
        event->ignore();
    }

    void CardUi::mouseReleaseEvent(QMouseEvent *event)
    {
        event->ignore();
    }

    void CardUi::mouseMoveEvent(QMouseEvent * event)
    {
        event->ignore();
    }
}// namespace Upgrade
